package com.lz.config.swagger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

/**
 * @Package com.lz.config.swagger
 * @Project currency-trading
 * @Author lenovo
 * @Date 2022/1/10 0:09
 * @Describe Swagger配置类-装配Swagger对象
 * @Version V1.0
 */
@Configuration //托管spring
@EnableSwagger2 //开启Swagger
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerConfig {

    private SwaggerProperties swaggerProperties;

    @Autowired
    public SwaggerConfig(SwaggerProperties swaggerProperties) {
        this.swaggerProperties = swaggerProperties;
    }

    @Bean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2) //文档格式
                .apiInfo(apiInfo())//增加API相关信息
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build(); //构建对象
        // 安全的配置(一般不需要，否则swagger测试时候，还需要先获取到token数据)
        docket.securitySchemes(securitySchemes()) //安全规则
                .securityContexts(securityContexts()); //安全配置的上下文
        return docket;
    }

    /**
     * API信息简介
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().contact(
                        new Contact(swaggerProperties.getName()
                                , swaggerProperties.getUrl()
                                , swaggerProperties.getEmail())
                ) //联系人信息
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
                .build();

    }

    /**
     * 安全的规则配置
     */
    private List<SecurityScheme> securitySchemes() {
        return Arrays.asList(new ApiKey("Authorization"
                , "Authorization"
                , "Authorization"));
    }

    /**
     * 安全的上下文
     */
    private List<SecurityContext> securityContexts() {
        return Arrays.asList(new SecurityContext(
                Arrays.asList(new SecurityReference("Authorization"
                        , new AuthorizationScope[]{new AuthorizationScope("global"
                        , "accessResource")})),
                PathSelectors.any()
        ));
    }
}
